package store.Service;

import store.Index;
import store.entry.Goods;
import java.util.ArrayList;
import java.util.Scanner;

public class Store {
    public static ArrayList<Goods> goodsArr = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    static {
        goodsArr.add(new Goods("10101", "海尔冰箱", 4999.9, 50));
        goodsArr.add(new Goods("10102", "美的冰箱", 3888.8, 50));
        goodsArr.add(new Goods("10103", "松下冰箱", 2999.9, 30));
        goodsArr.add(new Goods("10201", "天鹅冰箱", 1999.9, 20));
        goodsArr.add(new Goods("10202", "美的空调", 4888.8, 40));
        goodsArr.add(new Goods("10203", "美的吹风", 499.9, 49));
        goodsArr.add(new Goods("10301", "美的电灯", 49.9, 66));
        goodsArr.add(new Goods("10302", "美的电毯", 99.9, 73));
        goodsArr.add(new Goods("10303", "美的电壶", 49.9, 89));
    }
    public static void showGoods() {
        System.out.println("编号\t\t名称\t\t价格\t\t库存");
        for (Goods goods : goodsArr) {
            System.out.println(goods.getId() + "\t" + goods.getName() + "\t" + goods.getPrice() + "\t" + goods.getNum());
        }
    }
    //判断id
    public static boolean idRight(String id) {
        //判断id
        for (Goods goods : goodsArr) {
            //当商品id存在时，返回false结束方法的循环
            if (goods.getId().equals(id)) {
                return false;
            }
        }
        System.out.println("您输入的商品编号不存在");
        return true;
    }
    //判断数量
    public static boolean numRight(String id,int num) {
        boolean flag = false;
        if (num<=0){
            System.out.println("请输入正确的数量");
            return true;
        }
        for (Goods goods : goodsArr) {
            if (goods.getId().equals(id)) {
                //当商品数量不足时，返回true结束方法的循环
                if (goods.getNum() < num) {
                    System.out.println("商品数量不足,商品剩余："+goods.getNum());
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
    //购买商品
    public static void buyGoods() {
        showGoods();
        String id;
        int num;
        do {
            System.out.print("请输入要购买的商品编号：");
            id = sc.next();
        }while (idRight(id));
        do {
            System.out.print("请输入要添加的数量：");
            num = sc.nextInt();
        }while (numRight(id,num));
       outter:
       for (Goods goods : goodsArr) {
            if (goods.getId().equals(id)){
                System.out.println("您所要购买的商品信息如下：");
                System.out.println("编号\t\t名称\t\t价格\t\t购买数量");//输出商品信息
                System.out.println(goods.getId() + "\t" + goods.getName() + "\t" + goods.getPrice() + "\t" + num);
                System.out.println("添加成功");

                //购物车判断
                boolean flag = false;
                for (Goods goods1 : BuyCar.buyCarArr) {
                    if (goods1.getId().equals(id)) {
                        goods1.setNum(num+goods1.getNum());//修改商品数量
                        flag = true;
                    }
                }
                if (!flag){
                    BuyCar.buyCarArr.add(new Goods(id,goods.getName(),goods.getPrice(), num));//添加到购物车
                }

            }
        }
        System.out.println("您还需要继续购买吗？(是Y否任意键)");
        String choice = sc.next();
        if (choice.equals("Y")){
            buyGoods();
        }else {
            System.out.println("已返回");
            Index.Indexstore();
        }

    }

}
